//
//  WorkerViewController.m
//  ZYShang
//
//  Created by wangguangming on 15/8/3.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "WorkerViewController.h"
#import "WorkerView.h"
#import "Base.h"
#import "ReadyDiSignle.h"
#import "UserModel.h"
#import "UIImage+imagedown.h"
#import "Loadsingle.h"
#import "SchoolViewController.h"
#import "SearchWorkViewController.h"
#import "AddViewController.h"
#import "AddViewController.h"
@interface WorkerViewController ()<UIAlertViewDelegate,UITableViewDataSource,UITableViewDelegate>
{
    UIScrollView *_mainScroll;
    WorkerView *_itemView;
    int _row;
    
    UIView *_chooseView;
    
    UIView *_menuView;
    BOOL _isShit;
    
    NSString *_mktString;
    
   
}
//@property(nonatomic,strong)UITableView *mainTable;

@end



@implementation WorkerViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     _mainScroll.frame=CGRectMake(0, 0, WIDTH, HEIGHT);
    [[ReadyDiSignle signle] addObserver:self forKeyPath:@"workerArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
   
}
-(void)viewWillDisappear:(BOOL)animated
{
     [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"workerArray"];
    [super viewWillDisappear:animated];
   
    
}

-(void)passTrendValues:(NSString *)values

{
    self.mktidString=values;
        // NSLog(@"%@",values);
    [ReadyDiSignle signle].oidString=values;
    
    [[ReadyDiSignle signle] getAllwokers];
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
           selector:@selector(handleColorChange:)
               name:@"do"
             object:nil];
}
-(void)handleColorChange:(NSNotification *)sender
{
    if ([sender.name isEqualToString:@"do"]) 
        {
        [self shuju];
      }
    else
        {
        
        
        
        }
    
   
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBack];
    [ReadyDiSignle signle].oidString=self.mktidString;
    
    [[ReadyDiSignle signle] getAllwokers];
   
//    self.mainTable=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
//    [self.view addSubview:self.mainTable];
//    self.mainTable.delegate=self;
//    self.mainTable.dataSource=self;
    _isShit=NO;
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"人员管理";
    
    
    UserModel *user=[Loadsingle single].userArray[0];

    if (user.role.intValue==1)
        {
        UIBarButtonItem *right=[[UIBarButtonItem alloc] initWithTitle:@"更多" style:UIBarButtonItemStylePlain target:self action:@selector(moreAction)];
        self.navigationItem.rightBarButtonItem=right;

        }
    else
        {
        self.navigationItem.rightBarButtonItem=nil;
        }
    
    // Do any additional setup after loading the view.
}
-(void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary *)change context:(nullable void *)context
{
    if ([keyPath isEqualToString:@"workerArray"]) 
        {
        [self shuju];
//        [self.mainTable reloadData];
       
        }
}
//-(CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
//{
//    return 49+(WIDTH-10)/4;
//}
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{ 
//    
//   
//    return [ReadyDiSignle signle].workerArray.count;
//   
//}

   
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    static NSString *cellid=@"workcellid";
//    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];
//    if (cell==nil)
//        {
//        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
//        
//    
//        UserModel *user=[ReadyDiSignle signle].workerArray[indexPath.row];
//        _itemView=[[WorkerView alloc] initWithFrame:CGRectMake(5, 2, WIDTH-10, 45)];
//        _itemView.backgroundColor=[UIColor cyanColor];
//        _itemView.oidLaber.text=user.account;
//        if (user.photo==nil)
//            {
//            _itemView.photoImage.image=[UIImage imageNamed:@"default_userhead.jpg"];
//            }
//        else
//            {
//            _itemView.photoImage.image=[UIImage imageDownWithurlstring:user.photo imagebolock:^(UIImage *img) {
//                _itemView.photoImage.image=img;
//            }];
//            }
//            //        _itemView.photoImage.backgroundColor=[UIColor redColor];
//        [_itemView.logoButton setImage:[UIImage imageNamed:@"more_info_selected.png"] forState:UIControlStateNormal];
//        NSString *s=[self setrole:user.role.integerValue];
//        
//        _itemView.roleLaber.text=[NSString stringWithFormat:@"%@*%@*%@",user.mktName,user.dptName,s];
//        _itemView.nameLaber.text=user.name;
//        _itemView.phoneLaber.text=user.tel;
//       
//        [_itemView.logoButton addTarget:self action:@selector(buttonAtion:) forControlEvents:UIControlEventTouchUpInside];
//        [_itemView.logoButton setTag:110+indexPath.row];
//    [cell addSubview:_itemView];
//    }
//    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//
//    return cell;
//}
-(void)moreAction
{
    if (!_isShit)
        {
        _menuView=[[UIView alloc] initWithFrame:CGRectMake(WIDTH-125, 64, 120, 150)];
        
        _menuView.backgroundColor=[UIColor colorWithRed:217/256.0 green:217/256.0 blue:217/256.0 alpha:1];
        _menuView.layer.cornerRadius=6;
        NSArray *array=@[@"查询员工",@"选择学校",@"添加员工"];
        for (int i=0; i<3; i++)
            {
            UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(0, 0+50*i, 120, 50)];
            [button setTitle:array[i] forState:UIControlStateNormal];
            
            [_menuView addSubview:button];
            [button addTarget:self action:@selector(chooseAction:) forControlEvents:UIControlEventTouchUpInside];
            }
        
        [self.view addSubview:_menuView];
        _isShit=YES;
        }
    else
        {
        [_menuView removeFromSuperview];
        _isShit=NO;
        }
   
    
}
-(void)chooseAction:(UIButton *)sender
{
    if ([sender.titleLabel.text isEqualToString:@"查询员工"]) 
        {
        SearchWorkViewController *searchWorkViewController=[[SearchWorkViewController alloc] init];
        [self.navigationController pushViewController:searchWorkViewController animated:YES];
        
         [_menuView removeFromSuperview];
        }
   else if ([sender.titleLabel.text isEqualToString:@"选择学校"]) 
        {
        SchoolViewController *schoolViewController=[[SchoolViewController alloc] init];
        [self.navigationController pushViewController:schoolViewController animated:YES];
         [_menuView removeFromSuperview];
        }
   else if ([sender.titleLabel.text isEqualToString:@"添加员工"]) 
        {
        AddViewController *addViewController=[[AddViewController alloc] init];
        [self.navigationController pushViewController:addViewController animated:YES ];
         [_menuView removeFromSuperview];
        }
}
-(void)shuju
{
    
        // NSLog(@"%d",[ReadyDiSignle signle].workerArray.count);
    [_mainScroll removeFromSuperview];
    _mainScroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT)];
    _mainScroll.backgroundColor=COLOR;
    [self.view addSubview:_mainScroll];
    for (int i=0; i<[ReadyDiSignle signle].workerArray.count; i++)
        {
        UserModel *user=[ReadyDiSignle signle].workerArray[i];
        _itemView=[[WorkerView alloc] initWithFrame:CGRectMake(0, 5+(WIDTH/4+50)*i, WIDTH, WIDTH/4+45)];
        _itemView.backgroundColor=[UIColor whiteColor];
        _itemView.oidLaber.text=user.account;
        if (user.photo==nil)
            {
            _itemView.photoImage.image=[UIImage imageNamed:@"default_userhead.jpg"];
            }
        else
            {
        _itemView.photoImage.image=[UIImage imageDownWithurlstring:user.photo imagebolock:^(UIImage *img) {
            _itemView.photoImage.image=img;
        }];
            }
        if (user.type.intValue==1) {
            _itemView.typeLaber.text=@"全职";
        }
        if (user.type.intValue==2) {
            _itemView.typeLaber.text=@"兼职";
        }
        if (user.type.intValue==3) {
            _itemView.typeLaber.text=@"实习";
        }
        if (user.type.intValue==4) {
            _itemView.typeLaber.text=@"试用";
        }
        if(user.free.intValue==0)
            {
            _itemView.offLaber.text=@"上班";
            }
        else{
            _itemView.offLaber.text=@"下班";
        }
//        _itemView.photoImage.backgroundColor=[UIColor redColor];
        [_itemView.logoButton setImage:[UIImage imageNamed:@"shitios.png"] forState:UIControlStateNormal];
        _itemView.logoButton.imageView.contentMode=UIViewContentModeScaleAspectFit;
        NSString *s=[self setrole:user.role.integerValue];
        
        _itemView.roleLaber.text=[NSString stringWithFormat:@"%@*%@*%@",user.mktName,user.dptName,s];
        _itemView.nameLaber.text=user.name;
        _itemView.phoneLaber.text=user.tel;
        [_mainScroll addSubview: _itemView ];
        [_itemView.logoButton addTarget:self action:@selector(buttonAtion:) forControlEvents:UIControlEventTouchUpInside];
        [_itemView.logoButton setTag:110+i];
        }
    
        _mainScroll.contentSize=CGSizeMake(0, 64+(WIDTH/4+50)*([ReadyDiSignle signle].workerArray.count));
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)buttonAtion:(UIButton *)sender
{
    
    
    _row=sender.tag-110;
    UserModel *user=[Loadsingle single].userArray[0];
    
    UserModel *itemUser=[ReadyDiSignle signle].workerArray[_row];

    if (user.role.intValue==1)
        {
        [_chooseView removeFromSuperview];
        _chooseView=[[UIView alloc] initWithFrame:CGRectMake(WIDTH/2-60, HEIGHT/2-100, 120, 200)];
        _chooseView.backgroundColor=[UIColor colorWithRed:217/256.0 green:217/256.0 blue:217/256.0 alpha:1];
        _chooseView.layer.cornerRadius=6;
        NSArray *array;
        if (itemUser.enable.intValue==0)
            {
            array=@[@"修改信息",@"重置密码",@"解冻账号",@"员工离职",@"取消"];
            }
       else
           {
           array=@[@"修改信息",@"重置密码",@"冻结账号",@"员工离职",@"取消"];

           }
        for (int i=0; i<5; i++)
            {
            UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(0, 0+40*i, 120, 40)];
            [button setTitle:array[i] forState:UIControlStateNormal];
            [_chooseView addSubview:button];
            [button addTarget:self action:@selector(roleAction:) forControlEvents:UIControlEventTouchUpInside];
            }

        [self.view addSubview:_chooseView];
        }
    else if(user.role.intValue==2)
        {
        UIAlertView *a=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"重置密码" delegate:self cancelButtonTitle:@"YES" otherButtonTitles:@"NO", nil];
        [a show];
        }
        
    
    
    
//   UserModel *user=[ReadyDiSignle signle].workerArray[sender.tag-110];
//      
//   [[ReadyDiSignle signle] setPassWord:user.account]; 
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    if (buttonIndex==0)
        {
        UserModel *user=[ReadyDiSignle signle].workerArray[_row];
                  
        [[ReadyDiSignle signle] setPassWord:user.account]; 
      }
    else
        {
        
        }
    
}
-(void)roleAction:(UIButton *)sender
{
    UserModel *user=[ReadyDiSignle signle].workerArray[_row];
    [ReadyDiSignle signle].oidString=user._id;
    if ([sender.titleLabel.text isEqualToString:@"修改信息"]) 
        {
         [_chooseView removeFromSuperview];
        AddViewController *add=[[AddViewController alloc] init];
        add.usermodel=user;
        [self.navigationController pushViewController:add animated:YES];
        }
    else  if ([sender.titleLabel.text isEqualToString:@"重置密码"]) 
        {
        [[ReadyDiSignle signle] setPassWord:user.account];
        
         [_chooseView removeFromSuperview];
        }
    else  if ([sender.titleLabel.text isEqualToString:@"冻结账号"]) 
        {
        
        [[ReadyDiSignle signle] dongjieWorking];
         [_chooseView removeFromSuperview];
        }
    else  if ([sender.titleLabel.text isEqualToString:@"解冻账号"]) 
        {
        
        [[ReadyDiSignle signle] jiedongWorking];
        [_chooseView removeFromSuperview];
        }

    else  if ([sender.titleLabel.text isEqualToString:@"员工离职"]) 
        {
        
        [[ReadyDiSignle signle] lizhiWorking];
         [_chooseView removeFromSuperview];
        }
    else  if ([sender.titleLabel.text isEqualToString:@"取消"]) 
        {
        [_chooseView removeFromSuperview];
        }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
